package com.lanou.zy.projecta.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.volley.VolleySingleton;

/**
 * Created by dllo on 15/10/30.
 */
public class MainTitleFragment extends Fragment{
    private static final String ARGS_URL = "photo";
    private NetworkImageView mainTitleIv;
    private RequestQueue queue;
    private ImageLoader imageLoader;
    private String imgUrl;


    public static MainTitleFragment getInstance(String url) {
        MainTitleFragment mainTitleFragment = new MainTitleFragment();
        Bundle args = new Bundle();
        args.putString(ARGS_URL, url);
        mainTitleFragment.setArguments(args);

        return mainTitleFragment;
    }

    @Override
    public void onAttach(Context c) {
        super.onAttach(c);
        queue = Volley.newRequestQueue(c);
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            imgUrl = bundle.getString("imgUrl", imgUrl);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_maintitle, null);
        mainTitleIv = (NetworkImageView) v.findViewById(R.id.mainTitleIv);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        imageLoader = VolleySingleton.getInstance().getImageLoader();
        imgUrl = getArguments().getString(ARGS_URL);
        mainTitleIv.setImageUrl(imgUrl, imageLoader);

//        getBitmapCache(imgUrl, mainTitleIv);
    }

//    private void getBitmapCache(String url, ImageView imageView) {
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(
//                imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher
//        );
//        imageLoader.get(url, listener);
//    }
}
